package te.jetty.http.rest;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class CorsHandler extends AbstractHandler {  
	public CorsHandler(){
		System.out.println("实例化CorsHandler");
	}
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)  
            throws IOException, ServletException {  
    	response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		request.getSession(false);
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
    	response.setHeader("Access-Control-Max-Age", "3600");//cookie 过期时间
    	response.setHeader("Access-Control-Allow-Headers", "Content-Type, X-Token");
//      baseRequest.setHandled(true);  
    	//System.out.println("跨域处理");
    	System.out.println("调用前置handler");
    }  
}  